#Copyright (c) 2025, Alibaba Cloud and its affiliates;
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.



import json

import fce_util.libs.localrun as localrun
import fce_util.libs.common as common
import fce_util.libs.constants as constants
from fce_util.libs import log as loggers

logger = loggers.g_logger.logger



def tools_clean(args):
    """
    FCE Util deployed tools clean 
    """
    
    logger.debug('tools_clean enter,args:{}'.format(args))

    install_path = args.install_path if args.install_path else constants.FCE_BIN_PATH
    for tool in constants.FCE_TOOLS:
        if tool == 'operator':
            if not common.is_dir_exists(constants.KUBE_OPERATOR_DIR):
                stdout = '%s not exist' % constants.KUBE_OPERATOR_DIR
                logger.warn(stdout)
                continue
                #fce_res_common(success = False, errcode = 'Deploy.OperatorNotFound', errmsg = stdout)
            localrun.run('rm -rf %s' % constants.KUBE_OPERATOR_DIR)
            continue
        tool_path = install_path + '/' + tool
        if not common.is_file_exists(tool_path):
            stdout = '%s not exist' % tool_path
            logger.warn(stdout)
            #fce_res_common(success = False, errcode = 'Deploy.ToolNotFound', errmsg = stdout)
            continue
        localrun.run('rm -f %s' % tool_path)

    msg = {'Success': True}
    msg['Message'] = {
        'Msg': 'tools delete success'
    }
    print(json.dumps(msg))

def FCE_tools_clean(args):
    if args.clean == 'tool':
        tools_clean(args)
        return
    args.this_parser.print_help()

def make(parser):
    """
    FCE Tools Clean entry
    """
    parser.add_argument( 
        '--install_path',
        dest='install_path',
        nargs = '?',
        metavar = ('INSTALL_PATH'),
        help='Tools Install Path',
        required=False,
        )   
    
    parser.add_argument(
        '--tool_check',
        dest='tool_check',
        metavar = ('TOOL_CHECK'),
        help='Tools Check',
        required=False,
    )
    
    parser.set_defaults(
        func=FCE_tools_clean,
        this_parser=parser
    )
